package com.cyy.system.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import com.cyy.common.domain.Response;
import com.cyy.common.pojo.query.system.SystemArticleQuery;
import com.cyy.common.pojo.req.ArticlePushReq;
import com.cyy.common.pojo.req.ArticleSaveOrUpdateReq;
import com.cyy.common.pojo.req.RandomImgReq;
import com.cyy.common.pojo.vo.system.SystemArticleVo;
import com.cyy.common.service.ArticleService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @author caoyunyou
 */
@Tag(name = "文章")
@RestController
@RequestMapping("/system/article")
@RequiredArgsConstructor
public class ArticleController {

    private final ArticleService articleService;

    @Operation(description = "点赞")
    @GetMapping("/like/{id}")
    @SaCheckLogin
    public Response like(@PathVariable(name = "id") Integer id) {
        Integer res = articleService.like(id);
        return Response.success().data(res);
    }


    @Operation(summary = "分页展示文章DTO")
    @GetMapping("/page")
    @SaCheckLogin
    public Response getPage(@Validated SystemArticleQuery query) {
        return Response.success().data(articleService.getPage(query));
    }

    @Operation(description = "保存或者更新草稿")
    @PostMapping("/saveOrUpdate")
    @SaCheckLogin
    public Response saveOrUpdate(@RequestBody ArticleSaveOrUpdateReq req) {
        return Response.success().data(articleService.saveOrUpdateArticle(req));
    }

    @Operation(description = "编辑信息展示")
    @GetMapping("/editVo/{id}")
    @SaCheckLogin
    public Response editVoById(@PathVariable("id") Integer id) {
        SystemArticleVo res = articleService.queryEditVoById(id);
        return Response.success().data(res);
    }

    @Operation(description = "随机获取图片")
    @PostMapping("/randomImg")
    @SaCheckLogin
    public Response randomImg(@RequestBody RandomImgReq req) {
        return Response.success().data(articleService.randomImg(req));
    }

    @Operation(description = "发布文章")
    @PostMapping("/push")
    @SaCheckLogin
    public Response push(@RequestBody @Validated ArticlePushReq req) {
        articleService.push(req);
        return Response.success();
    }

    @Operation(description = "博客阅读校验，查询是否通过阅读验证")
    @GetMapping("/readVerify/{id}")
    public Response readVerifyQuery(@PathVariable("id") Integer id) {
        Boolean isPass = articleService.readVerifyQuery(id);
        return Response.success().data(isPass);
    }
}
